package com.ning.activitys.skysound.game;

import android.content.Context;
import android.graphics.Point;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.GridLayout;

import com.ning.activitys.skysound.activity.GameActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/24.
 */
public class GameView extends GridLayout{

    //定义一个二维数组
    private Card[][] cardMap = new Card[4][4];
    //Point是一个类型，记录的是有x y坐标的一个点
    private List<Point> emptyPoint = new ArrayList<>();

    public GameView(Context context) {
        super(context);
        initGameView();
    }

    public GameView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initGameView();
    }

    public GameView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initGameView();
    }

    private void initGameView() {

        //设置布局指明为4列
        setColumnCount(4);
        //设置背景颜色
        setBackgroundColor(0xffbbaba0);



        setOnTouchListener(new OnTouchListener() {

            private float startX, startY;
            private float offsetX, offSetY;//偏移

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        //event.getX()方法是获得当前触摸的坐标
                        startX = event.getX();
                        startY = event.getY();
                        break;
                    case MotionEvent.ACTION_UP:
                        offsetX = event.getX() - startX;
                        offSetY = event.getY() - startY;

                        //如果水平偏移量大于垂直偏移量说明是在水平方向移动
                        if (Math.abs(offsetX) > Math.abs(offSetY)) {
                            if (offsetX < 5) {
                                Log.e("offsetX", "左");
                                swipeLeft();
                            } else if (offsetX > -5) {
                                Log.e("offsetX", "右");
                                swipeRight();
                            }
                        } else {
                            //说明是在垂直方向上移动
                            if (offSetY > 5) {
                                Log.e("offsetX", "下");
                                swipDown();
                            } else if (offSetY < -5) {
                                Log.e("offsetX", "上");
                                swipeUp();
                            }
                        }
                        break;
                }

                //返回false会判断成Touch Down 没有触发成功，后续类型不会执行
                return true;
            }
        });
    }

    //当屏幕改变时获取屏幕宽高，再去限制无法横屏
    //方法：获取当前布局的宽高
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        //获得宽高中的最小值，适配屏幕
        int cardWidth = (Math.min(w, h) - 10) / 4;

        addCards(cardWidth, cardWidth);

        //
        startGame();
    }

    //添加卡片的方法
    private void addCards(int cardWidth, int cardHeight) {
        Card card;
        for (int y = 0; y < 4; y++) {
            for (int x = 0; x < 4; x++) {
                card = new Card(getContext());
                card.setNumber(0);
                addView(card, cardWidth, cardHeight);

                //把卡片加进二维数组
                cardMap[x][y] = card;
            }
        }
    }

    private void startGame() {

        //游戏开始的时候把分数清零
        GameActivity.getMainActivity().clearScore();

        //开始游戏的时候可能有的卡片的不是空值,所以对所有的卡片进行清空
        for (int y = 0; y < 4; y++) {
            for (int x = 0; x < 4; x++) {
                cardMap[x][y].setNumber(0);
            }
        }
        addRandomNumber();
        addRandomNumber();
    }

    //添加随机数的方法
    private void addRandomNumber() {
        //清空Point
        emptyPoint.clear();

        //遍历所有的位置
        //把所有的位置都放到emptyPoint数组里
        for (int y = 0; y < 4; y++) {
            for (int x = 0; x < 4; x++) {
                //只有是空点才能添加数字
                if (cardMap[x][y].getNumber() <= 0) {
                    emptyPoint.add(new Point(x, y));
                }
            }
        }
        //for循环执行完之后,随机的取出一个点
        //随机移除 1 - 16 Math.random是生成 0 - 1之间的随机数
        Point p = emptyPoint.remove((int) (Math.random() * emptyPoint.size()));
        //如果大于0.1返回2, 否则返回4. 出现2或者4的几率为 1:9
        cardMap[p.x][p.y].setNumber(Math.random() > 0.1 ? 2 : 4);

    }


    private void swipeLeft() {

        boolean merge = false;

        for (int y = 0; y < 4; y++) {
            for (int x = 0; x < 4; x++) {
                //循环 x+1 只走一次
                for (int x1 = x+1; x1 < 4; x1++){

                    if (cardMap[x1][y].getNumber() > 0){

                        if (cardMap[x][y].getNumber() <= 0){
                            cardMap[x][y].setNumber(cardMap[x1][y].getNumber());
                            cardMap[x1][y].setNumber(0);

                            x--;

                            merge = true;
                        }else if (cardMap[x][y].equals(cardMap[x1][y])){

                            cardMap[x][y].setNumber(cardMap[x][y].getNumber() * 2);
                            cardMap[x1][y].setNumber(0);
                            //有合并就有添加
                            GameActivity.getMainActivity().addScore(cardMap[x][y].getNumber());

                            merge = true;
                        }

                        break;
                    }
                }
            }
        }

//        for (int i = 0; i < 3; i++) {
//
//            for (int y = 0; y < 4; y++) {
//                for (int x = 0; x < 4; x++) {
//                    if (x != 3) {
//
//                        if (cardMap[x][y].getNumber() <= 0) {
//                            cardMap[x][y].setNumber(cardMap[x + 1][y].getNumber());
//                            cardMap[x + 1][y].setNumber(0);
//                        }
//                    }
//                }
//            }
//        }
//
//
//        for (int y = 0; y < 4; y++) {
//            for (int x = 0; x < 4; x++) {
//                if (x != 3) {
//                    if (cardMap[x + 1][y].equals(cardMap[x][y])) {
//                        int a = cardMap[x][y].getNumber();
//                        Log.e("int a",a+"");
//                        cardMap[x][y].setNumber(cardMap[x][y].getNumber() * 2);
//                        cardMap[x + 1][y].setNumber(0);
//
//                        if (a != 0){
//                            if (cardMap[x][y].getNumber() == (2 * a)){
//
//                                merge = true;
//                                Log.e("aa","zou" + cardMap[x][y].getNumber() + a);
//                            }
//                        }
//
//
//
//                    }
//                }
//            }
//        }
//
//        for (int i = 0; i < 3; i++) {
//
//            for (int y = 0; y < 4; y++) {
//                for (int x = 0; x < 4; x++) {
//                    if (x != 3) {
//
//                        if (cardMap[x][y].getNumber() <= 0) {
//                            cardMap[x][y].setNumber(cardMap[x + 1][y].getNumber());
//                            cardMap[x + 1][y].setNumber(0);
//                            merge = true;
//                        }
//                    }
//                }
//            }
//        }

        if (merge == true) {
            addRandomNumber();
        }
    }

    private void swipeRight() {

        boolean merge = false;

        for (int y = 0; y < 4; y++) {
            for (int x = 3; x >= 0; x--) {

                for (int x1 = x - 1; x1 >= 0; x1--) {
                    if (cardMap[x1][y].getNumber() > 0) {

                        if (cardMap[x][y].getNumber() <= 0) {
                            cardMap[x][y].setNumber(cardMap[x1][y].getNumber());
                            cardMap[x1][y].setNumber(0);

                            x++;

                            merge = true;

                        } else if (cardMap[x][y].equals(cardMap[x1][y])) {

                            cardMap[x][y].setNumber(cardMap[x][y].getNumber() * 2);
                            cardMap[x1][y].setNumber(0);

                            GameActivity.getMainActivity().addScore(cardMap[x][y].getNumber());

                            merge = true;

                        }
                        break;
                    }
                }
            }
        }

//        for (int i = 0; i < 3; i++) {
//
//            for (int y = 0; y < 4; y++) {
//                for (int x = 3; x >= 0; x--) {
//                    if (x != 0) {
//
//                        if (cardMap[x][y].getNumber() <= 0) {
//                            cardMap[x][y].setNumber(cardMap[x - 1][y].getNumber());
//                            cardMap[x - 1][y].setNumber(0);
//                        }
//                    }
//                }
//            }
//        }
//
//
//        for (int y = 0; y < 4; y++) {
//            for (int x = 3; x >= 0; x--) {
//                if (x != 0) {
//                    if (cardMap[x - 1][y].equals(cardMap[x][y])) {
//                        cardMap[x][y].setNumber(cardMap[x][y].getNumber() * 2);
//                        cardMap[x - 1][y].setNumber(0);
//                        merge = true;
//                    }
//                }
//            }
//        }
//
//        for (int i = 0; i < 3; i++) {
//
//            for (int y = 0; y < 4; y++) {
//                for (int x = 3; x >= 0; x--) {
//                    if (x != 0) {
//
//                        if (cardMap[x][y].getNumber() <= 0) {
//                            cardMap[x][y].setNumber(cardMap[x - 1][y].getNumber());
//                            cardMap[x - 1][y].setNumber(0);
//                        }
//                    }
//                }
//            }
//        }

        if (merge == true) {
            addRandomNumber();
        }

    }

    private void swipeUp() {

        boolean merge = false;

        for (int x = 0; x < 4; x++) {
            for (int y = 0; y < 4; y++) {

                for (int y1 = y + 1; y1 < 4; y1++) {
                    if (cardMap[x][y1].getNumber() > 0) {

                        if (cardMap[x][y].getNumber() <= 0) {
                            cardMap[x][y].setNumber(cardMap[x][y1].getNumber());
                            cardMap[x][y1].setNumber(0);

                            if (y != 0) {
                                y--;
                            }

                            Log.e("xy", x + " " + y);
                            merge = true;
                        } else if (cardMap[x][y].equals(cardMap[x][y1])) {

                            cardMap[x][y].setNumber(cardMap[x][y].getNumber() * 2);
                            cardMap[x][y1].setNumber(0);

                            GameActivity.getMainActivity().addScore(cardMap[x][y].getNumber());

                            merge = true;
                        }

                        break;
                    }
                }
            }
        }

//        for (int i = 0; i < 3; i++) {
//
//            for (int x = 0; x < 4; x++) {
//                for (int y = 0; y < 4; y++) {
//                    if (y != 3) {
//
//                        if (cardMap[x][y].getNumber() <= 0) {
//                            cardMap[x][y].setNumber(cardMap[x][y + 1].getNumber());
//                            cardMap[x][y + 1].setNumber(0);
//                        }
//                    }
//                }
//            }
//        }
//
//
//        for (int x = 0; x < 4; x++) {
//            for (int y = 0; y < 4; y++) {
//                if (y != 3) {
//                    if (cardMap[x][y + 1].equals(cardMap[x][y])) {
//                        cardMap[x][y].setNumber(cardMap[x][y].getNumber() * 2);
//                        cardMap[x][y + 1].setNumber(0);
//                        merge = true;
//                    }
//                }
//            }
//        }
//
//        for (int i = 0; i < 3; i++) {
//
//            for (int x = 0; x < 4; x++) {
//                for (int y = 0; y < 4; y++) {
//                    if (y != 3) {
//
//                        if (cardMap[x][y].getNumber() <= 0) {
//                            cardMap[x][y].setNumber(cardMap[x][y + 1].getNumber());
//                            cardMap[x][y + 1].setNumber(0);
//                        }
//                    }
//                }
//            }
//        }

        if (merge == true) {
            addRandomNumber();
        }

    }

    private void swipDown() {

        boolean merge = false;

        for (int x = 0; x < 4; x++) {
            for (int y = 3; y >= 0; y--) {

                for (int y1 = y - 1; y1 >= 0; y1--) {
                    if (cardMap[x][y1].getNumber() > 0) {

                        if (cardMap[x][y].getNumber() <= 0) {
                            cardMap[x][y].setNumber(cardMap[x][y1].getNumber());
                            cardMap[x][y1].setNumber(0);

                            if (y != 3) {
                                y++;
                            }


                            merge = true;
                        } else if (cardMap[x][y].equals(cardMap[x][y1])) {

                            cardMap[x][y].setNumber(cardMap[x][y].getNumber() * 2);
                            cardMap[x][y1].setNumber(0);

                            GameActivity.getMainActivity().addScore(cardMap[x][y].getNumber());

                            merge = true;
                        }
                    }
                }
            }
        }

//        for (int i = 0; i < 3; i++) {
//
//            for (int x = 0; x < 4; x++) {
//                for (int y = 3; y >=  0; y--) {
//                    if (y != 0) {
//
//                        if (cardMap[x][y].getNumber() <= 0) {
//                            cardMap[x][y].setNumber(cardMap[x][y - 1].getNumber());
//                            cardMap[x][y - 1].setNumber(0);
//                        }
//                    }
//                }
//            }
//        }
//
//
//        for (int x = 0; x < 4; x++) {
//            for (int y = 3; y >= 0; y--) {
//                if (y != 0) {
//                    if (cardMap[x][y - 1].equals(cardMap[x][y])) {
//                        cardMap[x][y].setNumber(cardMap[x][y].getNumber() * 2);
//                        cardMap[x][y - 1].setNumber(0);
//                        merge = true;
//                    }
//                }
//            }
//        }
//
//        for (int i = 0; i < 3; i++) {
//
//            for (int x = 0; x < 4; x++) {
//                for (int y = 3; y >= 0; y--) {
//                    if (y != 0) {
//
//                        if (cardMap[x][y].getNumber() <= 0) {
//                            cardMap[x][y].setNumber(cardMap[x][y - 1].getNumber());
//                            cardMap[x][y - 1].setNumber(0);
//                        }
//                    }
//                }
//            }
//        }


        if (merge == true) {
            addRandomNumber();
        }
    }
}
